package com.dzl.userwx;

import com.dzl.base.controller.BaseController;
import com.dzl.userwx.pojo.WelfareUserAddress;
import com.dzl.userwx.service.WelfareUserAddressService;
import com.dzl.userwx.pojo.dto.WelfareUserAddressDto;
import com.dzl.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.Map;


/**
 * 用户地址表(WelfareUserAddress)表控制层
 * @author ChenLong
 * @date 2021-06-05 16:26:38
 */
@Slf4j
@RestController
@RequestMapping("/welfareUserAddress")
public class WelfareUserAddressController extends BaseController {

    @Autowired
    private WelfareUserAddressService service;

    /**
     * 获取用户默认地址
     * @return com.dzl.util.JsonResult<com.dzl.useraddress.pojo.WelfareUserAddress>
     * @author ChenLong
     * @date 2021-06-05 16:57:25
     **/
    @GetMapping(value = "getUserAddress")
    public JsonResult<WelfareUserAddress> getUserAddress() {
        log.info("getUserAddress in ");
        Integer userId = getWxUserInfo().getId();
        Example welfareUserAddressExample = new Example(WelfareUserAddress.class);
        welfareUserAddressExample.createCriteria().andEqualTo("userId", userId);
        return JsonResult.success(service.selectOneByExample(welfareUserAddressExample));
    }

    /**
     * 更新用户默认地址
     * @param welfareUserAddressDto 用户地址实体类
     * @return com.dzl.util.JsonResult<com.dzl.useraddress.pojo.WelfareUserAddress>
     * @author ChenLong
     * @date 2021-06-07 16:09:11
     **/
    @PostMapping(value = "updateUserAddress")
    public JsonResult<Integer> updateUserAddress(@RequestBody WelfareUserAddressDto welfareUserAddressDto) {
        log.info("updateUserAddress in ");
        Integer userId = getWxUserInfo().getId();
        Map<String, Object> resMap = service.updateUserAddress(welfareUserAddressDto, userId);
        if (resMap.get("code").equals(1)) {
            return JsonResult.success();
        } else {
            return JsonResult.failWithMsg(resMap.get("msg").toString());
        }
    }
}
